﻿using System;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using Complaint.Models.Services.Interface;

namespace Complaint.Models.Services
{
    public class MailService : IMailService
    {
        public void Send(string to, string subject, string body)
        {
            try
            {
                var mailMessage = new MailMessage();
                mailMessage.To.Add(to);
                mailMessage.Subject = subject;
                mailMessage.Body = body;

                var smtpClient = new SmtpClient();
                smtpClient.SendCompleted += SendCompletedCallback;

                smtpClient.SendAsync(mailMessage, null);
            }
            catch (SmtpException smtpEx)
            {
                System.Diagnostics.Trace.TraceError(smtpEx.ToString());
            }
        }

        public void Send(String fromAddress, String toAddress, string subject, String messageBody)
        {
            Send(fromAddress, toAddress, subject, messageBody, false);
        }

        public void Send(String fromAddress, String toAddress, string subject, String messageBody, bool isBodyHtml)
        {
            try
            {
                var smtpClient = new SmtpClient();

                var message = new MailMessage { IsBodyHtml = isBodyHtml, Subject = subject, BodyEncoding = Encoding.Default };

                var addresses = toAddress.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                addresses.ToList().ForEach(s => message.To.Add(new MailAddress(s)));

                var messageFrom = new MailAddress(fromAddress);
                message.From = messageFrom;

                message.Body = messageBody;

                smtpClient.SendCompleted += SendCompletedCallback;

                smtpClient.SendAsync(message, null);
            }
            catch (SmtpException smtpEx)
            {
                System.Diagnostics.Trace.TraceError(smtpEx.ToString());
            }
        }

        public void Send(String fromAddress, String toAddress, string subject, String messageBody, String attachmentPath, bool isBodyHtml)
        {
            try
            {
                var smtpClient = new SmtpClient();

                var message = new MailMessage { IsBodyHtml = isBodyHtml, Subject = subject, BodyEncoding = Encoding.Default };

                var messageTo = new MailAddress(toAddress);
                message.To.Add(messageTo);

                var messageFrom = new MailAddress(fromAddress);
                message.From = messageFrom;

                message.Body = messageBody;

                if (!string.IsNullOrEmpty(attachmentPath) && File.Exists(attachmentPath))
                {
                    //Add Attachment
                    var attachFile = new Attachment(attachmentPath);
                    message.Attachments.Add(attachFile);
                }
                smtpClient.SendCompleted += SendCompletedCallback;

                smtpClient.SendAsync(message, subject);
            }
            catch (SmtpException smtpEx)
            {
                System.Diagnostics.Trace.TraceError(smtpEx.ToString());
            }
        }

        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
            var token = (string)e.UserState;

            if (e.Error != null)
            {
                var errMsg = string.Format("Error sending \"{0}\".\n{1}", token, e.Error);
                System.Diagnostics.Trace.TraceError(errMsg);
                var from = ConfigurationManager.AppSettings["CommentReporter"];
                var to = ConfigurationManager.AppSettings["Admin"];
                new MailService().Send(from, to, string.Format("Error sending \"{0}\"", token), errMsg);
            }
            else
            {
                // ToDo: handle message sent
            }
        }


    }
}